package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.entity.Cart;
import com.entity.Goods;
import com.entity.User;
import com.pojo.Address;
import com.service.GoodsService;
import com.service.ShopService;
import com.service.UserService;
import com.service.impl.CartserviceImpl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * @program: ssm
 * @description: 璐墿杞�
 * @author: zl
 * @create: 2022-05-17 14:56
 **/

@Controller
@RequestMapping("/cart")
public class CartController {

	@Autowired
	@Qualifier("cart")
	private CartserviceImpl cartservice;

	@Resource(name = "userSer") // 此处与UserServiceImpl的名称匹配
	private UserService userService;

	@Resource(name = "shopSer") // 此处与ShopServiceImpl的名称匹配
	private ShopService shopService;
	
	@Resource(name = "goodsService") // 此处与ShopServiceImpl的名称匹配
	private GoodsService goodsService;
	@RequestMapping("/all")
	public String selectAll(Model model, Integer userid) {
		List<Cart> carts = cartservice.selectCartByUserid(userid);
		int a = 0, b = 0;
		for (Cart cart : carts) {
			a += (cart.getPrice() * cart.getGood_num());
			b += cart.getGood_num();
			System.out.println(cart.getCart_id());
		}
		model.addAttribute("carts", carts);
		model.addAttribute("price", a);
		model.addAttribute("number", b);
		return "modcart";
	}
	
	//实现添加进购物车功能
	@ResponseBody
	@RequestMapping("/add/{userid}/{good_id}/{num}")
	public String addCart(@PathVariable("userid") Integer userid,@PathVariable("good_id") Integer good_id,@PathVariable("num") Integer num) {
		Goods goods = goodsService.selectGoodById(good_id);
		Cart cart=new Cart(1,num,goods.getGood_name(),goods.getShop_id(),(int)goods.getPrice(),good_id,userid,goods.getG_image());
		int res = cartservice.addCart(cart);
		if(res==1) {
			return "加入购物车成功！";
		}else {
			return "加入购物车失败！";
		}
		
	}

	@RequestMapping("/del") // 实现删除功能
	public ModelAndView delete(Integer cart_id) {
		int userid = cartservice.selectUserIdByCartId(cart_id);//获取用户id
		cartservice.deleteCart(cart_id);
		List<Cart> carts = cartservice.selectCartByUserid(userid);//获取用户所有购物车信息
		ModelAndView modelAndView = new ModelAndView("userManage");
		modelAndView.addObject("userinf", userService.selectUserById(userid));
		///ProfessionalDesign/orderdetail?order_id='+data
		modelAndView.addObject("href", 4);
		return modelAndView;
	}

	@RequestMapping("/update/{cart_id}/{good_num}")
	public String update(@PathVariable("cart_id") int cart_id, @PathVariable("good_num") int good_num) {
		cartservice.update(cart_id, good_num);
		return "redirect:/cart/all";
	}
	
	//跳转至查看订单页面
	@ResponseBody
	@RequestMapping("settlement")
	public ModelAndView settlement(String cartdata) {// 1.根据cart_id找到user_id
		System.out.println("参数接受" + cartdata);
		List<Cart> listCart = JSON.parseArray(cartdata, Cart.class);
		List<Cart> cartList = new ArrayList<Cart>();// 用于存放购物车对象
		for (Cart cart : listCart) {
			Cart tempCart = cartservice.selectCartByCartId(cart.getCart_id()); // 获取原始Cart对象
			tempCart.setGood_num(cart.getGood_num());
			cartList.add(tempCart);
		}
		int user_id = cartservice.selectUserIdByCartId(listCart.get(0).getCart_id());// 得到用户ID
		List<Address> addresses = userService.selectAddressById(user_id);// 得到用户所有地址
		User user = userService.selectUserById(user_id);// 得到用户对象
		System.out.println(user);
		System.out.println(addresses);
		System.out.println(cartList);
		ModelAndView mav = new ModelAndView("settlement");
		mav.addObject("user",user);
		mav.addObject("addresses",addresses);
		mav.addObject("cartList",cartList);
		return mav;
	}
}
